﻿/* 
  Copyright (c) SilverLining 2015
  Date:    2016-1-19
  File:    DispatchEntry.cs  
  Description: 
    Implementation of the dispatch table entry.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SL.M.Collections
{
    /// <summary>
    /// A dispatch table entry provides a mapping of message type and sender to a functor for an individual process.
    /// </summary>
    public class DispatchEntry : IEquatable<DispatchEntry>
    {
        /// <summary>
        /// Return the key.
        /// </summary>
        public DispatchKey Key
        {
            get;
            private set;
        }

        /// <summary>
        /// Return the functor.
        /// </summary>
        public Func<object,bool> Target
        {
            get;
            private set;
        }
        
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="key">The dispatch key.</param>
        /// <param name="func">The dispatch functor.</param>
        public DispatchEntry(DispatchKey key, Func<object,bool> func)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (func == null)
            {
                throw new ArgumentNullException("func");
            }

            Key = key;
            Target = func;
        }


        /// <summary>
        /// Create a dispatch entry using the specified arguments.
        /// </summary>
        /// <param name="source">The dispatch source.</param>
        /// <param name="message">The dispatch message.</param>
        /// <param name="func">The dispatch function.</param>
        /// <returns>A dispatch entry.</returns>
        public static DispatchEntry Create<T>(IDispatchSource source, MessageType message, Func<T, bool> func) where T : class
        {
            return Create(new DispatchKey(source, message), func);
        }

        /// <summary>
        /// Create a dispatch entry using the specified arguments.
        /// </summary>
        /// <param name="key">The dispatch key.</param>
        /// <param name="func">The dispatch functor.</param>
        /// <returns>A dispatch entry.</returns>
        public static DispatchEntry Create<T>(DispatchKey key, Func<T,bool> func) where T : class
        {
            return new DispatchEntry(key, (o) => func(o as T));
        }

        /// <summary>
        /// Return if this entry is equal to another object.
        /// </summary>
        /// <param name="obj">The object to compare against.</param>
        /// <returns>True if the objects are equal.</returns>
        public override bool Equals(object obj)
        {
            if (obj is DispatchEntry)
            {
                return Equals((DispatchEntry)obj);
            }
            return base.Equals(obj);
        }

        /// <summary>
        /// Return a hash code for this entry.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return Key.GetHashCode();
        }

        /// <summary>
        /// Return if this entry is equal to another entry.
        /// </summary>
        /// <param name="other">The entry to compare against.</param>
        /// <returns>True if the entries are equal.</returns>
        public bool Equals(DispatchEntry other)
        {
            if (other != null)
            {
                return Key.Equals(other.Key);
            }
            return false;
        }
    }
}
